//
//  MHTabBar.m
//  微博
//
//  Created by mh on 16/3/20.
//  Copyright © 2016年 mh. All rights reserved.
//

#import "FSTabBar.h"
#define count 5
@interface FSTabBar ()
@property(nonatomic,weak)UIButton *plusBtn;
@end

@implementation FSTabBar
//初始化  添加加号按钮
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //添加需要的按钮
        UIButton *plus = [UIButton buttonWithType:UIButtonTypeCustom];
//        [plus setImage:[UIImage imageNamed:@"button_write"] forState:UIControlStateNormal];
        [plus setBackgroundImage:[UIImage imageNamed:@"button_write"] forState:UIControlStateNormal];
        plus.size = plus.currentBackgroundImage.size;
        [plus addTarget:self action:@selector(plusOnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:plus];
        self.plusBtn = plus;
    }
    return self;
}
//加号按钮点击，用Block传递出去
- (void)plusOnClick:(UIButton *)btn{
    if (self.plusBtnOnClick) {
        //加号按钮点击，   -->block
        self.plusBtnOnClick();
    }
}
//重新布局  在系统布局完之后再布局
- (void)layoutSubviews{
    [super layoutSubviews];
    self.plusBtn.centerX = self.width * 0.5;
    self.plusBtn.centerY = self.height * 0.5;
    //在系统布局完后 自己布局子控件
    __block NSInteger index = 0;
    __block CGFloat btnX = 0;
    CGFloat btnW = self.width / count;
    [self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        //系统内部除了 按钮还有背景图片和分割线
        if ([obj isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            if (index == 2) {
                index++;
            }
            btnX = index * btnW;
            obj.x = btnX;
            index++;
        }
    }];
}

@end
